package com.example.springbolgdemo.common.interceptor;

import com.example.springbolgdemo.common.util.JwtUtils;
import com.example.springbolgdemo.contstant.Contstants;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         //从handler中获取token
         String userToken=request.getHeader(Contstants.HEADER_TOKEN);
         log.info("从header中获取token: "+userToken);
         //校验token
         Claims claims = JwtUtils.parseToken(userToken);
         if (claims==null){
             //token不合法
             response.setStatus(401);
             return false;
         }
         return true;
    }


}
